package com.tianqu.tms.www.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.www.user.actionform.UserUpdateActionForm;
import com.tianqu.tms.www.user.viewhelper.UserDetailViewHelper;

/**
 * 跳转至用户修改确认页
 * 
 * @author tianshifu
 *
 */
public class UserUpdateConfirmAction extends TmsAction {
	
	private Logger logger = Logger.getLogger(UserUpdateConfirmAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		UserUpdateActionForm uiForm = (UserUpdateActionForm)form;
		
		UserDetailViewHelper view = new UserDetailViewHelper();
		
//		User user =new User();
		//用户唯一性验证
//		if(user.findUserInfoByUserId(uiForm.getLoginName(),Integer.parseInt(uiForm.getId()))==false)
//		{
//			setErrorsToRequest(request, TmsConstants.IC001 ,null);
//			return mapping.getInputForward();				
//		}
		
		//将form表单信息存到viewhelper中
		setViewhelperFromForm(view,uiForm);
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		
		//将信息放到uc中用于提交操作
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	private UserDetailViewHelper setViewhelperFromForm(UserDetailViewHelper view,UserUpdateActionForm form) throws TmsDaoException{
		view.setId(form.getId());
//		view.setLoginName(form.getLoginName());
//		view.setUserName(form.getUserName());
		UserClass code = UserClass.getInstance();
		//从字典表中查出用户类型信息
		view.setUserType(code.getUserClassNameByUserClass(Integer.parseInt(form.getUserType())));
		
		return view;
	}

}
